package ar.com.sgsvm.view.cliente;

import java.util.List;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import ar.com.sgsvm.R;
import ar.com.sgsvm.entity.Cliente;
import ar.com.sgsvm.logic.ClienteLogica;
import ar.com.sgsvm.view.cliente.DialogOrdenCliente.OrdenarClienteListener;

public class FrmListadoClientes extends FragmentActivity implements OrdenarClienteListener {

	private ClienteAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lyt_listado_clientes);
		ListView lstClientes = (ListView) findViewById(R.id.listadoClientes);
		lstClientes.setTextFilterEnabled(true);
		ClienteLogica clienteLogica = new ClienteLogica();
		try {
			List<Cliente> clientes = clienteLogica.listadoClientes();
			mAdapter = new ClienteAdapter(getApplicationContext(), R.layout.lyt_cliente, clientes);
			lstClientes.setAdapter(mAdapter);
		} catch (Exception e) {
			// TODO: handle exception
		}
		lstClientes.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				DialogOkCancel dialogOkCancel = new DialogOkCancel();
				dialogOkCancel.show(getSupportFragmentManager(), "dialog");
			}
		});
		EditText txtFilter = (EditText) findViewById(R.id.txtFilter);
		txtFilter.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				mAdapter.getFilter().filter(arg0);
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub

			}
		});
		registerForContextMenu(lstClientes);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		menu.setHeaderTitle("Elija una opcion");
		inflater.inflate(R.menu.context_menu, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.nuevo:
			Toast.makeText(this, "item 0", Toast.LENGTH_SHORT).show();
			break;
		case R.id.ordenar:
			DialogOrdenCliente dialogOrdenCliente = new DialogOrdenCliente();
			dialogOrdenCliente.show(getSupportFragmentManager(), "dialogOrdenar");
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		int position = info.position;
		Cliente clienteSeleccionado = mAdapter.getItem(position);
		switch (item.getItemId()) {
		case R.id.mnEliminar:
			Toast.makeText(this, "eliminar " + clienteSeleccionado.toString(), Toast.LENGTH_SHORT).show();
			return true;
		case R.id.mnLlamar:
			Toast.makeText(this, "llmar " + clienteSeleccionado.toString(), Toast.LENGTH_SHORT).show();
			return true;
		case R.id.mnModificar:
			Toast.makeText(this, "modificar " + clienteSeleccionado.toString(), Toast.LENGTH_SHORT).show();
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}

	@Override
	public void ordenarClientes(int which, int asc) {
		mAdapter.sort(which, asc);
	}

}
